<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<title>drag test</title>

</head>
<body>
<div id="test">
</div>
<script src="../../base/javascript/kissy.js"></script>
<script>
    KISSY.ready(function(S) {
        var UA = S.UA,Event = S.Event,Node = S.Node,
                iframe = new S.Node("<iframe></iframe>");

        iframe.on("load", function() {
            iframe.detach();
            var win = iframe[0].contentWindow,
                    doc = win.document,body = doc.body;
            window.doc = doc;
            doc.open();
            doc.write("<style>\
    /*firefox 破碎图片手动定义才出现*/\
img:-moz-broken {\
    -moz-force-broken-image-icon: 1;\
    height: 24px;\
    width: 24px;\
}\
</style><p>可以把桌面图片拖过来</p><p>可以把桌面图片拖过来</p>");
            doc.close();
            body = doc.body;
            setTimeout(function() {
                if (UA.gecko || UA.opera) {
                    body.contentEditable = true;
                }
                else if (UA.webkit)
                    body.parentNode.contentEditable = true;
                else
                    doc.designMode = 'on';
            }, 0);


            Event.on(doc, "dragenter dragover", function(ev) {
                ev.halt();
                ev = ev.originalEvent;
                var dt = ev.dataTransfer;
            });
            Event.on(doc, "drop", function(ev) {
                ev.halt();
                ev = ev.originalEvent;
                S.log(ev);
                var dt = ev.dataTransfer;
                dt.dropEffect = "copy";
                var files = dt.files;
                setTimeout(function() {
                    var archor,ap;
                    /**
                     * firefox 会自动添加节点
                     */
                    if (UA.gecko) {
                        S.all("img", doc).each(function(el) {
                            S.log(el[0].hasAttribute("_moz_dirty"));
                            if (el[0].hasAttribute("_moz_dirty")) {
                                archor = el[0].nextSibling;
                                ap = el[0].parentNode;
                                el.remove();
                            }
                        });
                    } else {
                        //空行里拖放肯定没问题，其他在文字中间可能不准确
                        ap = doc.elementFromPoint(ev.clientX, ev.clientY);
                        archor = ap.lastChild;
                    }


                    if (!files) return;
                    for (var i = 0; i < files.length; i++) {
                        var file = files[i],name = file.name,size = file.size;
                        var img = new Node("<img " +
                                "src='http://img02.taobaocdn.com/tps/i2/T16WJqXaXeXXXXXXXX-32-32.gif'" +
                                "/>", null, doc);
                        ap && ap.insertBefore(img[0], archor);
                    }
                }, 10);

            });

        });
        iframe.appendTo(S.one("#test"));
    });
</script>
</body>
</html>